<!--
 * Copyright © 2023
 * @Author: zhangxiang
 * @Date: 2023-07-23 17:33:25
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-07-26 10:49:51
 * @Description:
 *
-->

<template>
  <div class="vm-config">
    <VMFormAliyun
      v-if="parentCloudProvider === '阿里云'"
      ref="createForm"
      :scene="'orchestrate'"
      :info="form"
    />
    <VMFormHuawei
      v-if="parentCloudProvider === '华为云'"
      ref="createForm"
      :scene="'orchestrate'"
      :info="form"
    />
    <VMFormOpenstack
      v-if="parentCloudProvider === 'Openstack'"
      ref="createForm"
      :scene="'orchestrate'"
      :info="form"
    />
  </div>
</template>

<script>
import commonMixin from './commonMixin.js'
import VMFormAliyun from './child/VMFormAliyun.vue'
import VMFormHuawei from './child/VMFormHuawei.vue'
import VMFormOpenstack from './child/VMFormOpenstack.vue'

export default {
  components: {
    VMFormAliyun,
    VMFormHuawei,
    VMFormOpenstack
  },
  mixins: [commonMixin],
  props: ['params', 'GraphApi', 'cell'],
  data() {
    return {
      form: {
        vmName: '', // 主机名称
        chargeType: '', // 计费模式
        period: '',
        periodUnit: '',
        cpRegionId: '', // 区域
        zone: '', // 可用区
        imageId: '', // 镜像
        flavorId: '', // 规格
        sysDiskType: '', // 系统盘
        sysDiskSize: 40, // 系统盘大小
        loginPwd: '', // 登录密码
        vmTags: [] // 标签
      }
    }
  },
  mounted() {
    this.form = Object.assign(
      {},
      this.form,
      this.params.attribute || {},
      this.parentAttribute
    )
  },
  methods: {
    /**
     * @description: 保存配置
     * @param {*} callback
     * @return {*}
     */
    saveConfig(callback) {
      this.$refs.createForm.validateForm((form) => {
        const businessData = JSON.parse(JSON.stringify(this.params))
        businessData.attribute = form
        this.GraphApi.setObjectData(
          this.params.id,
          JSON.stringify(businessData)
        )
        this.cell.setValue(businessData.attribute.vmName || '虚拟机')
        this.GraphApi.refresh(this.cell)
        callback()
      })
    }
  }
}
</script>

